package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.domain.Orders;
import com.ssm.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("/findAll/{pageNum}/{pageSize}")
    public ModelAndView findAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        ModelAndView mv = new ModelAndView();
        List<Orders> list = ordersService.findAll(pageNum, pageSize);
        System.out.println(list);
        System.out.println("====");
        for (Orders orders : list) {
            System.out.println(orders);
        }
//        PageInfo就是一个分页bean
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
//        mv.addObject("ordersList",list);
//        mv.setViewName("orders-list");
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable String id) {
        ModelAndView mv = new ModelAndView();
        Orders orders = ordersService.findById(id);
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }

}
